import orjson
import pandas as pd


# 语言对象
class LanguageObject(object):
    def __init__(self, chinese, english, traditional_chinese, thai):
        self.chinese = chinese
        self.english = english
        self.traditional_chinese = traditional_chinese
        self.thai = thai

    def __str__(self):
        return ("{\"chinese\":\"%s\",\"english\":\"%s\",\"traditionalChinese\":\"%s\",\"thai\":\"%s\"}"
                %
                (self.chinese, self.english, self.traditional_chinese, self.thai))

    # 该方法用于转 json 字符串
    def encode_language_object(self):
        return {
            'chinese': self.chinese,
            'english': self.english,
            'traditionalChinese': self.traditional_chinese,
            'thai': self.thai}


# 读取 excel 表格数据，并进行遍历
# header = 0 ，第 1 行为表字段
pd_read_excel = pd.read_excel("file\\枚举_2024_04_12.xlsx", sheet_name=0, header=None)

# 遍历每一行数据
language_list = []
for index, row in pd_read_excel.iterrows():
    # 第一列
    chinese = row[0]
    # 第二列
    english = row[1]
    # 第三列
    traditional_chinese = row[2]
    # 第四列
    thai = row[3]
    if isinstance(chinese, float) or isinstance(english, float) or isinstance(traditional_chinese, float) or isinstance(
            thai, float):
        pass
    else:
        # print("{", f"\"chinese\":\"{chinese}\" , "
        #            f"\"english\":\"{english}\" , "
        #            f"\"traditionalChinese\":\"{traditional_chinese}\" , "
        #            f"\"thai\":\"{thai}\"", "}")
        language_obj = LanguageObject(chinese=chinese, english=english, traditional_chinese=traditional_chinese,
                                      thai=thai)
        # print(language_obj, ",", sep="")
        language_list.append(language_obj)

print(len(language_list))
option = orjson.OPT_APPEND_NEWLINE | orjson.OPT_SERIALIZE_NUMPY
json_dumps = orjson.dumps(language_list,
                          option=option,
                          default=LanguageObject.encode_language_object)

# 写入到 enums.json 文件当中
with open("file\\enum.json", 'wb') as file:
    file.write(json_dumps)

print(json_dumps)
